package cn.dankal.flyaudio.presenter;

import androidx.annotation.NonNull;
import api.ShopUserServiceFactory;
import cn.dankal.basiclib.api.ShopUserService;
import cn.dankal.basiclib.pojo.BaseResponse;
import cn.dankal.basiclib.pojo.CommentsEntity;
import cn.dankal.basiclib.pojo.req.SubCommentsReq;
import cn.dankal.basiclib.rx.AbstractSubscriber;
import cn.dankal.flyaudio.contract.CommentsDetailContract;
import cn.dankal.flyaudio.contract.EvaluateListContract;

/**
 * <pre>
 *     e-mail : junfeng.nie@duntech.com.cn
 *     time   : 28/7/2020
 *     desc   :
 *     version: cn.dankal.flyaudio.personal
 *     Copyright: Copyright（c）2018
 *     Company:
 * </pre>
 *
 * @author niejunfeng
 */
public class EvaluateListPresenter implements EvaluateListContract.Presenter {
    private EvaluateListContract.View mView;
    @Override
    public void getEvaluate(String uuid) {
        mView.showLoadingDialog();

        ShopUserServiceFactory.getCommentDetail(uuid)
                .subscribe(new AbstractSubscriber<BaseResponse<CommentsEntity>>(mView) {
                    @Override
                    public void onNext(BaseResponse<CommentsEntity> commentsEntityBaseResponse) {
                        mView.dismissLoadingDialog();
                        mView.evaluateSuccess(commentsEntityBaseResponse.getData());
                    }
                });
    }

    @Override
    public void like(String uuid) {
        mView.showLoadingDialog();
        ShopUserServiceFactory.likeCommentsProduct(uuid)
                .subscribe(new AbstractSubscriber<BaseResponse<Object>>(mView) {
                    @Override
                    public void onNext(BaseResponse<Object> objectBaseResponse) {
                        mView.dismissLoadingDialog();
                        mView.likeSuccess();
                    }
                });
    }

    @Override
    public void unLike(String uuid) {
        mView.showLoadingDialog();
        ShopUserServiceFactory.unLikeCommentsProduct(uuid)
                .subscribe(new AbstractSubscriber<BaseResponse<Object>>(mView) {
                    @Override
                    public void onNext(BaseResponse<Object> objectBaseResponse) {
                        mView.dismissLoadingDialog();
                        mView.unLikeSuccess();
                    }
                });
    }

    @Override
    public void sendMessage(String parentUuid, String content) {
        mView.showLoadingDialog();
        SubCommentsReq subCommentsReq = new SubCommentsReq();
        subCommentsReq.setContent(content);
        subCommentsReq.setParentUuid(parentUuid);
        subCommentsReq.setParentType("PRODUCT_COMMENT");
        ShopUserServiceFactory.subComments(subCommentsReq)
                .subscribe(new AbstractSubscriber<BaseResponse<Object>>(mView) {
                    @Override
                    public void onNext(BaseResponse<Object> objectBaseResponse) {
                        mView.dismissLoadingDialog();
                        mView.sendSuccess();
                    }
                });
    }

    @Override
    public void attachView(@NonNull EvaluateListContract.View view) {
        this.mView = view;
    }

    @Override
    public void detachView() {

        this.mView = null;
    }
}
